package com.itheima.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtils1 {
    private static SqlSessionFactory factory=null;

    static{
        try {
            InputStream is = Resources.getResourceAsStream("mybatis.xml");
            factory = new SqlSessionFactoryBuilder().build(is);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession openSession(){
        return openSession(false);
    }
    public static SqlSession openSession(boolean autoCommit){
        SqlSession sqlSession = factory.openSession(autoCommit);
        return sqlSession;
    }
    public static void commit(SqlSession sqlsession){
        if (sqlsession!=null){
            sqlsession.commit();
        }
    }
    public static void rollback(SqlSession sqlsession){
        if (sqlsession!=null){
            sqlsession.rollback();
        }
    }
    public static void close(SqlSession sqlsession){
        if (sqlsession!=null){
            sqlsession.close();
        }
    }


}
